package Command;

import java.io.File;
import java.io.FileNotFoundException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;

import Algo.Algo;
import DataBase.MenuTag_File;
import DirectsDB.MenuDirects;


/**
* Description : the Thread of init:
* <p>
*
*@author Danielle
*/
public class threadOfInit extends Thread {
	
	private MenuTag_File dataBase;
	private MenuDirects dirDB;
	private int running=1; //0-die, 1-run , 2-pause
	private boolean end=false;
	private int precent=0;
	ArrayList<Algo>  algoList;
	
	/**
	* Description Ctor
	* @param MenuTag_File dataBase
	* @param MenuDirects dirDB
	* @param ArrayList<Algo>  algo
	*/
	public threadOfInit(MenuTag_File dataBase,MenuDirects dirDB ,ArrayList<Algo>  algo)
	{
		this.dataBase=dataBase;
		this.dirDB=dirDB;
		algoList=algo;
	}
	
	/**
	* Description the run function 
	*/
	public void run() {
		String[] strFilesDB=dataBase.getFiles();
		String[] strDirDB=dirDB.retAllDir();
		String temp;
		int i,j=0, sizeFiles=strFilesDB.length, sizeDirect=strDirDB.length;
		/// run over all the Directs and files:
		for(i=0; i<(sizeFiles+sizeDirect); i++)
		{
			 ///the present:
			 end=false;
			 precent=((100*i)/(sizeFiles+sizeDirect));
			 try {
				Thread.sleep(600);
			} catch (InterruptedException e1) {
				e1.printStackTrace();
			}
			///if we not die and didnt end
			 while( running!=0 && (!end) ) 
			 {
				 	 ///if we in running (1) state:
					 if(running!=2)
			    	{
						
						 	
						 	 ///we are in the place of strFilesDB
							 if(i<sizeFiles)
							 {
								 temp=strFilesDB[i];
								 temp=temp.replaceAll("////////", "////");
								 ///check the file :
								 checkFiles(temp);
								 end=true;
							 }
							 ///we start the strDirDB
							 else if(i==sizeFiles)
							 {
								j=0;
								temp=strDirDB[j];
								temp=temp.replaceAll("////////", "////");
								try {
									///check the direct: 
									checkDir(temp);
								} catch (FileNotFoundException e) {
									e.printStackTrace();
								}
								///run over j
								j++;
								end=true;
						 	 }
							 ///we in strDirDB
							 else 
							 {
								temp=strDirDB[j];
								temp=temp.replaceAll("////////", "////");
								try {
									///check the direct: 
									checkDir(temp);
								} catch (FileNotFoundException e) {
									e.printStackTrace();
								}
								///run over j
								j++;
								end=true;
							 }
				    }
			 }
			
	    }
		///in the end: 
		precent=98;
		 try {
				Thread.sleep(800);
			} catch (InterruptedException e1) {
				e1.printStackTrace();
			}
		/// change to present 99
		precent=99;
		System.out.println("END: INIT");
	}
	
	
	/**
	* Description check Files in DB
	* @param String str
	*/
	public void checkFiles(String str)
	{
		///check if the file is exist:
		boolean exists = (new File(str)).exists(); 
		///if not delete it from the DB
		if (!exists)
		{
			String temp=str;
			temp=temp.replaceAll("////","////////");
			dataBase.delFile(temp);
		}
	}
	
	/**
	* Description check Directs in DB
	* @param String str
	* @throws FileNotFoundException
	*/
	public void checkDir(String str) throws FileNotFoundException
	{
		List<File> files;
		File fileTemp;
		String strFile;
		///check if the direct is exist - if not delete it from DB
		boolean exists = (new File(str)).exists(); 
		if (!exists)
		{
			String temp=str;
			temp=temp.replaceAll("////","////////");
			dirDB.delDir(temp);
		}
		else
		{
			///if the directs is exist- find all the files and directs in it:
			DirectCommand dirCmd=new DirectCommand(algoList, dataBase);
			files=walkingForInit.getFileListing(new File(str));
			/// run over the list:
			for(int i=0; i<files.size(); i++)
			{
				///add the files and if it direct send it Recursive
				fileTemp=files.get(i);
				strFile=""+str+"\\"+fileTemp.getName();
				
				Path dir = Paths.get(strFile);
				dirCmd.addFile(dir);
			}
				
			
		}
		
	}
	
	
	/**
	* Description kill the thread
	*/
	public void die()
	{
		running=0;
	}
	
	/**
	* Description continue the thread
	*/
	public void cont()
	{
		running=1;
	}
	
	/**
	* Description pause the thread
	*/
	public void pause()
	{
		running=2;
	}
	
	/**
	* Description getPrecent of work
	* @return int
	*/
	public int getPrecent()
	{
		return precent;
	}
	

}
